package org.ddd.book.interfaces.controller;

import lombok.extern.slf4j.Slf4j;
import org.ddd.book.domain.DomainException;
import org.ddd.book.interfaces.BaseResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice
@RestController
@Slf4j
public class ExceptionController {

    @ExceptionHandler(value = {Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResult<String> defaultException(Exception e) {
        log.error("ExceptionController", e);
        return BaseResult.fail("system error,please try agan!");
    }
    
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    public BaseResult<String> defaultIllegalArgumentException(IllegalArgumentException e) {
        return BaseResult.fail(e.getMessage());
    }

    @ExceptionHandler(value = DomainException.class)
    @ResponseStatus(HttpStatus.OK)
    public BaseResult<String> domainException(DomainException e) {
        return BaseResult.fail(e.getMessage());
    }


}
